 /*!
 * Buttons helper for fancyBox
 * version: 1.0.2
 * @requires fancyBox v2.0 or later
 *
 * Usage: 
 *     $(".fancybox").fancybox({
 *         buttons: {
 *             position : 'top'
 *         }
 *     });
 * 
 * Options:
 *     tpl - HTML template
 *     position - 'top' or 'bottom'
 * 
 * 
 */
(function ($) {
	//Shortcut for fancyBox object
	var F = $.fancybox;

	//Add helper object
	F.helpers.buttons = {
			defaults : {
				skipSingle : false, // disables if gallery contains single image
				position   : 'top', // 'top' or 'bottom'
				tpl        : '<div id="fancybox-buttons"><ul><li><a class="btnPrev" title="Previous" href="javascript:;"></a></li><li><a class="btnPlay" title="Start slideshow" href="javascript:;"></a></li><li><a class="btnNext" title="Next" href="javascript:;"></a></li><li><a class="btnToggle" title="Toggle size" href="javascript:;"></a></li><li><a class="btnClose" title="Close" href="javascript:;"></a></li></ul></div>'
			},
		tpl: '<div id="fancybox-buttons"><ul><li><a class="btnPrev" title="Previous" href="javascript:;"></a></li><li><a class="btnPlay" title="Start slideshow" href="javascript:;"></a></li><li><a class="btnNext" title="Next" href="javascript:;"></a></li><li><a class="btnToggle" title="Toggle size" href="javascript:;"></a></li><li><a class="btnClose" title="Close" href="javascript:jQuery.fancybox.close();"></a></li></ul></div>',
		list: null,
		buttons: {},

		update: function () {
			var toggle = this.buttons.toggle.removeClass('btnDisabled btnToggleOn');

			//Size toggle button
			if (F.current.canShrink) {
				toggle.addClass('btnToggleOn');

			} else if (!F.current.canExpand) {
				toggle.addClass('btnDisabled');
			}
		},

		beforeLoad: function (opts) {
			//Remove self if gallery do not have at least two items
			if (F.group.length < 2) {
				F.coming.helpers.buttons = false;
				F.coming.closeBtn = true;

				return;
			}

			//Increase top margin to give space for buttons
			F.coming.margin[ opts.position === 'bottom' ? 2 : 0 ] += 30;
		},

		onPlayStart: function () {
			if (this.list) {
				this.buttons.play.attr('title', 'Pause slideshow').addClass('btnPlayOn');
			}
		},

		onPlayEnd: function () {
			if (this.list) {
				this.buttons.play.attr('title', 'Start slideshow').removeClass('btnPlayOn');
			}
		},

		afterShow: function (opts, obj) {
			var buttons = this.buttons;

			if (!buttons) {
				this.list = $(opts.tpl).addClass(opts.position).appendTo('body');

				buttons = {
						  next: function ( direction ) {  
					            var current = F.current;  
					            rad = 0;//加入重置rad值  
					  
					            if (current) {  
					                if (!isString(direction)) {  
					                    direction = current.direction.next;  
					                }  
					  
					                F.jumpto(current.index + 1, direction, 'next');  
					            }  
					        },  
					  
					        // Navigate to previous gallery item  
					        prev: function ( direction ) {  
					            var current = F.current;  
					            rad = 0;//加入重置rad值  
					  
					            if (current) {  
					                if (!isString(direction)) {  
					                    direction = current.direction.prev;  
					                }  
					  
					                F.jumpto(current.index - 1, direction, 'prev');  
					            }  
					        } ,
					play   : this.list.find('.btnPlay').click( F.play ),
					toggle : this.list.find('.btnToggle').click( F.toggle ),
					close  : this.list.find('.btnClose').click( F.close ),
					rotateL : this.list.find('.btnRotateL').click( F.rotateL ),//增加左转单击事件
					rotateR : this.list.find('.btnRotateR').click( F.rotateR )//增加右转单击事件
				}
			}

			//Prev
			if (obj.index > 0 || obj.loop) {
				buttons.prev.removeClass('btnDisabled');
			} else {
				buttons.prev.addClass('btnDisabled');
			}

			//Next / Play
			if (obj.loop || obj.index < obj.group.length - 1) {
				buttons.next.removeClass('btnDisabled');
				buttons.play.removeClass('btnDisabled');

			} else {
				buttons.next.addClass('btnDisabled');
				buttons.play.addClass('btnDisabled');
			}

			this.buttons = buttons;

			this.onUpdate(opts, obj);
		},

		onUpdate: function () {
			this.update();
		},
		rotateL : function(){  
            var f_w = $("#fancybox-content").width();  
            var f_h = $("#fancybox-content").height();  
            var translate = (f_h - f_w)/2;  
            rad=rad-90;  
            if(rad==-360){  
                rad = 0;  
            }  
            //if($.browser.msie){  
            //  if($.browser.version=="7.0"||$.browser.version=="8.0"){  
            //     F.rotIsIE(f_w,f_h,translate);  
            //  }else{  
            //      F.rot(f_w,f_h,translate);  
            //  }  
            //}else{  
            //  F.rot(f_w,f_h,translate);  
            //}  
            F.rot(f_w,f_h,translate);  
    },  

    rotateR : function(){  
            var f_w = $("#fancybox-content").width();  
            var f_h = $("#fancybox-content").height();  
            var translate = (f_h - f_w)/2;  
            rad=rad+90;  
            if(rad==360){  
                rad = 0;  
            }  
            //if($.browser.msie){  
            //  if($.browser.version=="7.0"||$.browser.version=="8.0"){  
            //     F.rotIsIE(f_w,f_h,translate);  
            //  }else{  
            //      F.rot(f_w,f_h,translate);  
            //  }  
            //}else{  
            //  F.rot(f_w,f_h,translate);  
            //}  
            F.rot(f_w,f_h,translate);  
    },  

    rot : function(f_w,f_h,translate){  
            if(rad/90%2){  
                $("#fancybox-img").css("-webkit-transform","translate("+(translate-4)+"px,"+(-translate)+"px)"+" rotate("+rad+"deg)").css("-moz-transform","translate("+(translate-4)+"px,"+(-translate)+"px)"+" rotate("+rad+"deg)").css("-o-transform","translate("+(translate-4)+"px,"+(-translate)+"px)"+" rotate("+rad+"deg)").css("-ms-transform","translate("+(translate-4)+"px,"+(-translate)+"px)"+" rotate("+rad+"deg)").css("transform","translate("+(translate-4)+"px,"+(-translate)+"px)"+" rotate("+rad+"deg)");  
                $("#fancybox-img").height(f_h).width(f_w);  
                $("#fancybox-content").height(f_w).width(f_h);  
                $("#fancybox-wrap").width($("#fancybox-content").width()+20);  
                var left = $("#fancybox-wrap").css("left");  
                var leftvalue = left.substring(0,left.length - 2);  
                var tempvalue = ($("#fancybox-content").height()-$("#fancybox-content").width())/2;  
                var pianyivalue = parseInt(leftvalue) + parseInt(tempvalue);  
                $("#fancybox-wrap").css("left",pianyivalue+"px");  
            }else{  
                $("#fancybox-img").css("-webkit-transform","rotate("+rad+"deg)").css("-moz-transform","rotate("+rad+"deg)").css("-o-transform","rotate("+rad+"deg)").css("-ms-transform","rotate("+rad+"deg)").css("transform","translate(-4px, 0px) rotate("+rad+"deg)");  
                $("#fancybox-img").height("100%").width("100%");  
                $("#fancybox-content").height(f_w).width(f_h);  
                $("#fancybox-wrap").width($("#fancybox-content").width()+20);  
                var left = $("#fancybox-wrap").css("left");  
                var leftvalue = left.substring(0,left.length - 2);  
                var tempvalue = ($("#fancybox-content").height()-$("#fancybox-content").width())/2;  
                var pianyivalue = parseInt(leftvalue) + parseInt(tempvalue);  
                $("#fancybox-wrap").css("left",pianyivalue+"px");  
            }  
    },  
      
    rotIsIE : function (f_w,f_h,translate){  
            if(rad/90%2){  
                F.progidIE();  
                $("#fancybox-img").height(f_h).width(f_w);  
                $("#fancybox-content").height(f_w).width(f_h);  
                $("#fancybox-wrap").width($("#fancybox-content").width()+20);  
                var left = $("#fancybox-wrap").css("left");  
                var leftvalue = left.substring(0,left.length - 2);  
                var tempvalue = ($("#fancybox-content").height()-$("#fancybox-content").width())/2;  
                var pianyivalue = parseInt(leftvalue) + parseInt(tempvalue);  
                $("#fancybox-wrap").css("left",pianyivalue+"px");  
            }else{  
                F.progidIE();  
                $("#fancybox-img").height("100%").width("100%");  
                $("#fancybox-content").height(f_w).width(f_h);  
                $("#fancybox-wrap").width($("#fancybox-content").width()+20);  
                var left = $("#fancybox-wrap").css("left");  
                var leftvalue = left.substring(0,left.length - 2);  
                var tempvalue = ($("#fancybox-content").height()-$("#fancybox-content").width())/2;  
                var pianyivalue = parseInt(leftvalue) + parseInt(tempvalue);  
                $("#fancybox-wrap").css("left",pianyivalue+"px");  
            }  
    },  

    progidIE : function(){  
            alert(rad);  
            switch(rad/90){  
               case -3:  
               $("#fancybox-img").css("filter","progid:DXImageTransform.Microsoft.BasicImage(rotation=1)");  
               break;  
               case -2:  
               $("#fancybox-img").css("filter","progid:DXImageTransform.Microsoft.BasicImage(rotation=2)");  
               break;  
               case -1:  
               $("#fancybox-img").css("filter","progid:DXImageTransform.Microsoft.BasicImage(rotation=3)");  
               break;  
               case 0:  
               $("#fancybox-img").css("filter","progid:DXImageTransform.Microsoft.BasicImage(rotation=0)");  
               break;  
               case 1:  
               $("#fancybox-img").css("filter","progid:DXImageTransform.Microsoft.BasicImage(rotation=1)");  
               break;  
               case 2:  
               $("#fancybox-img").css("filter","progid:DXImageTransform.Microsoft.BasicImage(rotation=2)");  
               break;  
               case 3:  
               $("#fancybox-img").css("filter","progid:DXImageTransform.Microsoft.BasicImage(rotation=3)");  
               break;  
            }  
    },
		beforeClose: function () {
			if (this.list) {
				this.list.remove();
			}

			this.list = null;
			this.buttons = {};
		}
	};

}(jQuery));